/**
 * 弹出窗操作
 * */
layui.define(["layer", "config"], function (exports) {
    let layer = layui.layer;
    let config = layui.config;

    let sdWindow = {
        open: function(view) {
            if (!view.area) {
                view.area = (view.type == 2) ? ["360px", "300px"] : "360px"
            }
            if (!view.offset) {
                view.offset = "35px"
            }
            if (view.fixed == undefined) {
                view.fixed = false
            }
            view.resize = view.resize != undefined ? view.resize : false;
            view.shade = view.shade != undefined ? view.shade : 0.5;

            let end = view.end;
            view.end = function() {
                layer.closeAll("tips");
                end && end()
            };
            if (view.url) {
                (view.type == undefined) && (view.type = 1);
                let success = view.success;
                view.success = function(s, t) {
                    let url = view.url;
                    if (config.version != undefined) {
                        if (url.indexOf("?") == -1) {
                            url += "?v="
                        } else {
                            url += "&v="
                        }
                        if (config.version == true) {
                            url += new Date().getTime()
                        } else {
                            url += config.version
                        }
                    }
                    dialog.showLoading(s, 2);
                    $(s).children(".layui-layer-content").load(url, function() {
                        success ? success(s, t) : "";
                        dialog.removeLoading(s, false)
                    })
                }
            }
            return layer.open(view)
        },
        showLoading: function(s, r, q) {
            let html = ['<div class="ball-loader"><span></span><span></span><span></span><span></span></div>', '<div class="rubik-loader"></div>'];
            if (!s) {
                s = "body"
            }
            if (r == undefined) {
                r = 1
            }
            $(s).addClass("page-no-scroll");
            let t = $(s).children(".page-loading");
            if (t.length <= 0) {
                $(s).append('<div class="page-loading">' + html[r - 1] + "</div>");
                t = $(s).children(".page-loading")
            }
            q && t.css("background-color", "rgba(255,255,255," + q + ")");
            t.show()
        },
        removeLoading: function(q, s, p) {
            if (!q) {
                q = "body"
            }
            if (s == undefined) {
                s = true
            }
            let r = $(q).children(".page-loading");
            if (p) {
                r.remove()
            } else {
                s ? r.fadeOut() : r.hide()
            }
            $(q).removeClass("page-no-scroll")
        },
        popupRight: function(popup) {
            if (popup.title == undefined) {
                popup.title = false;
                popup.closeBtn = false
            }
            if (popup.anim == undefined) {
                popup.anim = 2
            }
            if (popup.fixed == undefined) {
                popup.fixed = true
            }
            popup.isOutAnim = false;
            popup.offset = "r";
            popup.shadeClose = true;
            popup.area = "336px";
            popup.skin = "layui-layer-adminRight";
            popup.move = false;
            return dialog.open(popup)
        },
        routerNotFound: function() {
            layer.open({
                title: false,
                skin: "layui-layer-admin",
                btn: [],
                offset: "30px",
                anim: 6,
                shadeClose: true,
                time: 3000,
                area: '360px',
                offset: 'rt',
                shade: 0,
                closeBtn: 0,
                anim: 2,
                resize: false,
                move: false,
                btn: [],
                icon: 2,
                content: "你无权访问该路由或此" + location.hash + "路由不存在"
            })
        },
        closeThisDialog: function() {
            parent.layer.close(parent.layer.getFrameIndex(window.name))
        },
        closeDialog: function(dialog) {
            let id = $(dialog).parents(".layui-layer").attr("id").substring(11);
            layer.close(id)
        },
        rightView: function (name) {
            let url = $(this).attr("data-url");
            dialog.popupRight({
                id: "layer-notice",
                url: url ? url : "components/common/" + name + ".html"
            })
        },
        popupCenter: function (param) {
            let $height = (typeof param.height) == "undefined" ? '80%' : (param.height == 0 ? '80%' : param.height);
            let $width = (typeof param.width) == "undefined" ? '600px' : param.width;
            let $area = [$width, $height];
            let id = "soda" + parseInt((Math.random() * 1000000000)+"");
            let popupCenterIndex = layer.open({
                type: 1,
                id: id,
                title: param.title ? param.title : false,
                shade: .5,
                offset: '50px',
                area: param.area ? param.area : $area,
                resize: false,
                scrollbar: false,
                skin: param.skin ? param.skin : "soda-class",
                success: function () {
                    let $id = '#'+id;
                    let $close = '#'+id + ' .close';
                    $($id).load(param.path + '?t=' + Math.random(), function (r) {
                        $($close).click(function () {
                            layer.close(popupCenterIndex);
                        });
                        layui.form.render();
                        param.init ? param.init() : '';
                    });
                },
                btn: param.btn ? param.btn : ['操 作', '关 闭'],
                yes: function (index, layero) {
                    param.opt ? param.opt(index) : '';
                },
                btn2: function (index, layero) {

                },
                end: function () {
                    param.end ? param.end() : '';
                }
            });
            return popupCenterIndex;
        },
        info: function (param) {
            dialog.popupCenter({
                title: '数据详情',
                height: (typeof param.height) == "undefined" ? '80%' : param.height,
                width: (typeof param.width) == "undefined" ? '660px' : param.width,
                path: '/components/common/info.html',
                btn: [],
                init: function () {
                    param.init ? param.init() : '';
                }
            });
        },
        form: function (param) {
            dialog.popupCenter({
                title: param.title,
                height: (typeof param.height) == "undefined" ? '80%' : param.height,
                width: (typeof param.width) == "undefined" ? '600px' : param.width,
                path: param.path,
                init: function () {
                    param.init ? param.init() : '';
                },
                opt: function () {
                    param.opt ? param.opt() : '';
                }
            });
        },
        finishWin: function (page, fn) {
            layer.close(page);
            fn ? fn() : '';
        },
    }
    exports('sdWindow', sdWindow);
})